import jsonServer from 'json-server'

const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()

// 设置默认中间件 (logger, static, cors and no-cache)
server.use(middlewares)

// 添加自定义路由
server.use(jsonServer.rewriter({
  '/api/system/user': '/sys_user',
  '/api/system/user/:id': '/sys_user/:id',
  '/api/system/role': '/sys_role',
  '/api/system/role/:id': '/sys_role/:id',
  '/api/system/menu': '/sys_menu',
  '/api/system/menu/:id': '/sys_menu/:id',
  '/api/system/dept': '/sys_dept',
  '/api/system/dept/:id': '/sys_dept/:id',
  '/api/system/user-role': '/sys_user_role',
  '/api/system/role-menu': '/sys_role_menu',
  '/api/system/role-dept': '/sys_role_dept'
}))

// 使用默认路由
server.use(router)

// 启动服务器
const port = 3000
server.listen(port, () => {
  console.log(`JSON Server is running on http://localhost:${port}`)
  console.log('\n可用的API端点:')
  console.log('用户管理: http://localhost:3000/api/system/user')
  console.log('角色管理: http://localhost:3000/api/system/role')
  console.log('菜单管理: http://localhost:3000/api/system/menu')
  console.log('部门管理: http://localhost:3000/api/system/dept')
  console.log('用户角色关联: http://localhost:3000/api/system/user-role')
  console.log('角色菜单关联: http://localhost:3000/api/system/role-menu')
  console.log('角色部门关联: http://localhost:3000/api/system/role-dept')
  console.log('\n直接访问数据:')
  console.log('http://localhost:3000/sys_user')
  console.log('http://localhost:3000/sys_role')
  console.log('http://localhost:3000/sys_menu')
  console.log('http://localhost:3000/sys_dept')
  console.log('http://localhost:3000/sys_user_role')
  console.log('http://localhost:3000/sys_role_menu')
  console.log('http://localhost:3000/sys_role_dept')
})